/* global window */
import modelExtend from 'dva-model-extend'
import queryString from 'query-string'
import { del, get, list, set } from 'services/exchangerate'
import * as ExchangeSet from 'services/exchangeset'
import { message } from 'antd'
import * as basicData from 'services/basicData.js'
import { pageModel } from './common'

export default modelExtend(pageModel, {
  namespace: 'exchangerate',
  state: {
    currenciesTypeData: [],
  },
  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/exchangerate') {
          const payload = queryString.parse(location.search) || { page: 1 }
          dispatch({
            type: 'list',
            payload,
          })
          dispatch({
            type: 'initSelectData',
          })
        }
      })
    },
  },
  effects: {
    * list ({ payload = {} }, { call, put }) {
      const data = yield call(list, payload)
      const listData = data.data
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: listData.data,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: listData.total,
            },
          },
        })
      }
    },
    * initSelectData ({ payload }, { call, put }) {
      const currenciesData = yield call(basicData.listall, { type: 'Currencies' })
      if (currenciesData.success) {
        yield put({
          type: 'updateState',
          payload: {
            currenciesTypeData: currenciesData.data,
          },
        })
      }
    },

    // 修改数据, 将修该的数据推给后台, 后台确认成功后再显示成功,成功后才关闭对话框
    * update ({ payload }, { select, call, put }) {
      const id = yield select(({ exchangerate }) => exchangerate.currentItem.id)
      const newData = { ...payload, id }
      const data = yield call(ExchangeSet.edit, newData)
      if (data.success) {
        message.success('update success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list' })
      } else {
        message.error('update failed')
        throw data
      }
    },
    * delete ({ payload }, { call, put, select }) {
      const data = yield call(del, { id: payload })
      const { selectedRowKeys } = yield select(_ => _.exchangerate)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
        yield put({ type: 'list' })
      } else {
        message.error('delete failed')
        throw data
      }
    },

    * create ({ payload }, { call, put }) {
      payload = {
        id: 1,
        ...payload,
      }
      const data = yield call(ExchangeSet.add, payload)
      if (data.success) {
        message.success('create success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list' })
      } else {
        message.error('create failed')
        throw data
      }
    },
    * initSelectData ({ payload }, { call, put }) {
      const currenciesData = yield call(basicData.listall, { type: 'Currencies' })
      if (currenciesData.success) {
        yield put({
          type: 'updateState',
          payload: {
            currenciesTypeData: currenciesData.data,
          },
        })
      }
    },
    * showModal ({ payload }, { call, put }) {
      const { currentItem: { currencyCodeFrom, currencyCodeTo } } = payload
      const data = yield call(ExchangeSet.list, { currencyCodeFrom, currencyCodeTo })
      if (data.success) {
        yield put({
          type: 'updateModalState',
          payload: {
            modalType: 'update',
            currentItem: data.data.data[0],
            modalVisible: true,
          },
        })
      } else {
        throw data
      }
    },
  },

  reducers: {
    updateSelectState (state, { payload }) {
      return { ...state, ...payload }
    },
  },
})
